home *** CD-ROM | disk | FTP | other *** search
- /*
- File: GradientImportScripting.c
-
- Copyright ⌐ 1990, Thomas Knoll.
- Copyright ⌐ 1993-6, Adobe Systems Incorporated.
- All rights reserved.
-
- C source file for scripting functions for import example.
- */
-
- #include "GradientImport.h"
-
- /*****************************************************************************/
- /* Opens up the scripting parameters to begin reading commands. */
-
- void OpenScriptParams (GPtr globals)
- {
- PIDescriptorHandle subHandle = NULL;
- DescriptorKeyID key = 0;
- DescriptorTypeID type = 0;
- int16 loop = 0;
- int32 flags = 0;
- OSErr gotErr = noErr, stickyError = noErr;
- Boolean invert = false, returnValue = true, leaveEarly = false;
-
- if (DescriptorAvailable())
- {
- gToken = OpenReader(NULL);
- /* don't know how many we're going to be passed, so don't bother with array */
- if (gToken)
- {
- while (!leaveEarly)
- {
- leaveEarly = PIGetKey(gToken, &key, &type, &flags);
- switch (key)
- {
- case keyMultiImportCount:
- PIGetCount(gToken, &(gCount));
- leaveEarly = true;
- break;
- /* We're going to ignore all other keys. What we're looking for is our
- list, which will be preceded by our count key. Once we find it, we'll drop
- out, to eventually call our Read routine. */
- }
- }
- }
- gQueryForParameters = PlayDialog();
- }
- /* return true if want to show our Dialog */
- }
-
- /*****************************************************************************/
-
- /* Reads the next batch of values and sets the global parameters. */
-
- void ReadScriptParams (GPtr globals)
- {
- int16 loop = 0;
- int32 flags = 0;
- DescriptorTypeID type = 0;
- DescriptorTypeID passType = classMultiImportStruct;
- DescriptorKeyID key = 0;
- DescriptorKeyIDArray subKeyIDArray =
- { keyRows, keyColumns,
- keyOurMode, 0 }; /* keyInvert */
- PIDescriptorHandle subHandle = NULL;
- PIReadDescriptor subToken = NULL;
- OSErr stickyError = noErr;
-
- if (DescriptorAvailable())
- {
- if (gToken)
- {
- if (gCount > 0)
- {
- gLastInvert = false; // if we've got a valid descriptor, assume NO invert
- PIGetObj(gToken, &passType, &subHandle);
- subToken = OpenReadDesc(subHandle, subKeyIDArray);
- if (subToken)
- {
- SwitchScriptInfo (globals, subToken);
- stickyError = CloseReadDesc(subToken); // done.
- subToken = NULL; // just in case
- PIDisposeHandle(subHandle); // dispose.
- subHandle = NULL; // just in case.
-
- if (stickyError)
- {
- if (stickyError == -1715) // missedParamErr
- ; /* missing parameter. Walk keyIDArray to find which one. */
- else
- gResult = stickyError; // real error occured
- }
- gContinueImport = true; // we got something, so keep going!
- }
- gCount--;
- }
- if (gCount < 1)
- CloseScriptParams(globals); // close em up!
- } /* no readToken */
- } /* not correct descriptorParameters version */
- }
-
- /*****************************************************************************/
-
- /* Checks the parameters against scripting-returned parameters, if any, and
- updates our parameters to match ones given to us by the scripting system. */
-
- void CloseScriptParams (GPtr globals)
- {
- OSErr stickyError = noErr;
-
- if (DescriptorAvailable())
- {
- if (gToken)
- {
- stickyError = CloseReader(&gToken); // done.
-
- if (stickyError)
- {
- if (stickyError == errMissingParameter)
- ; /* missing parameter. Check our parameters and coerce if need be. */
- else
- gResult = stickyError; // real error occured
- }
- }
- }
- gCount = 0; /* reset this puppy */
- gContinueImport = false;
- }
-
-
- /*****************************************************************************/
- /* Updates globals from scripting parameters */
-
- void SwitchScriptInfo (GPtr globals, PIReadDescriptor token)
- {
- double x;
- OSType mode = ourRGBColorMode;
- const double minRows = kRowsMin, maxRows = kRowsMax,
- minColumns = kColumnsMin, maxColumns = kColumnsMax;
- unsigned long pixelsUnitPass = unitPixels;
- DescriptorKeyID key = 0;
- DescriptorTypeID type = 0;
- int16 loop = 0;
- int32 flags = 0;
- int32 count = 0;
- Boolean invert = false;
-
- while (PIGetKey(token, &key, &type, &flags))
- {
- switch (key)
- {
- case keyRows:
- PIGetPinUnitFloat(token, &minRows, &maxRows, &pixelsUnitPass, &x);
- gLastRows = (short)x;
- break;
- case keyColumns:
- PIGetPinUnitFloat(token, &minColumns, &maxColumns, &pixelsUnitPass, &x);
- gLastCols = (short)x;
- break;
- case keyOurMode:
- PIGetEnum(token, &mode);
- gLastMode = GetPlugInMode(mode);
- break;
- case keyInvert:
- PIGetBool(token, &invert);
- gLastInvert = invert;
- break;
- }
- }
- }
-
- /*****************************************************************************/
-
- /* Checks for any data then writes our parameters to the scripting system. */
-
- OSErr CheckAndWriteScriptParams (GPtr globals)
- {
- OSErr gotErr = noErr;
-
- if (gLastImages) gotErr = WriteScriptParams(globals);
- else gResult = userCanceledErr; // standard return from multiple acquire
-
- return gotErr;
- }
-
- /*****************************************************************************/
-
- /* Writes our parameters to the scripting system. */
-
- OSErr WriteScriptParams (GPtr globals)
- {
- OSType mode = GetGradientMode(gLastMode);
- const double rows = gLastRows, columns = gLastCols;
- Boolean invert = gLastInvert;
- uint32 count = gLastImages;
- PIWriteDescriptor token = NULL;
- OSErr stickyError = noErr;
-
- if (DescriptorAvailable())
- {
- token = OpenWriter();
- if (token)
- {
- PIPutCount(token, keyMultiImportCount, count);
- for (count = 0; count < (uint32)gLastImages; count++)
- {
- PIPutObj(token, keyMultiImportInfo, classMultiImportStruct, &gArray [count]);
- /* routine disposes handle when its done with it */
- }
-
- gLastImages = 0; // reset
- stickyError = CloseWriter(&token); // returns descriptor and sets plugInDialogOptional
- }
-
- }
- return stickyError;
- }
-
- /*****************************************************************************/
- /* Returns a descriptor from our parameters */
-
- void CreateDescriptor (GPtr globals)
- {
- OSType mode = GetGradientMode(gLastMode);
- const double rows = gLastRows, columns = gLastCols;
- Boolean invert = gLastInvert;
- PIWriteDescriptor token = NULL;
- PIDescriptorHandle h;
- OSErr stickyError = noErr;
-
- if (DescriptorAvailable())
- {
- token = OpenWriter();
- if (token)
- {
- PIPutUnitFloat(token, keyRows, unitPixels, &rows);
- PIPutUnitFloat(token, keyColumns, unitPixels, &columns);
- PIPutEnum(token, keyOurMode, typeGradientMode, mode);
- if (invert) PIPutBool(token, keyInvert, invert);
-
- stickyError = CloseWriteDesc(token, &h);
- token = NULL;
-
- if (!stickyError)
- {
- if (gLastImages >= kMaxDescriptors)
- gLastImages--; // just keep replacing last one
-
- gArray [gLastImages++] = h;
-
- gArray [gLastImages] = h = NULL; // just in case
- /* don't forget to dispose of this after storing with PutObject! */
- }
- }
- }
- }
-
- /*****************************************************************************/
- /* Takes the plug-in mode and returns the GradientMode */
-
- OSType GetGradientMode (short plugInMode)
-
- {
- switch (plugInMode)
- {
- case plugInModeBitmap:
- return ourBitmapMode;
- break;
- case plugInModeGrayScale:
- return ourGrayscaleMode;
- break;
- case plugInModeIndexedColor:
- return ourIndexedColorMode;
- break;
- case plugInModeRGBColor:
- default:
- return ourRGBColorMode;
- break;
- }
- }
-
- /*****************************************************************************/
- /* Takes the GradientMode and returns the plug-in mode */
-
- short GetPlugInMode (OSType gradientMode)
-
- {
- switch (gradientMode)
- {
- case ourBitmapMode:
- return plugInModeBitmap;
- break;
- case ourGrayscaleMode:
- return plugInModeGrayScale;
- break;
- case ourIndexedColorMode:
- return plugInModeIndexedColor;
- break;
- case ourRGBColorMode:
- default:
- return plugInModeRGBColor;
- break;
- }
- }
-
- /*****************************************************************************/
-